package com.china08.yxyapi.api.manage.tec.teaching.zujuan.zujuanlist;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.paper.Paper;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.PaperRepository;
import com.china08.yxyapi.security.profile.YxyProfile;

/**
 * 组卷列表
 * 
 */
@CrossOrigin
@RestController
public class ZujuanListFindController extends YxyApiBaseController {

    @Autowired
    private PaperRepository paperRepository;

    @RequestMapping(value = "/api_yw/manage/tec/teaching/zujuan/zujuanlist/find", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Page<RespModel> process(HttpServletRequest request, //
            HttpServletResponse response, //
            @RequestBody ReqModel reqModel) {
        YxyProfile yxyProfile = getProfile(request, response);
        String userId = yxyProfile.getId();
        String schoolId = yxyProfile.getSchoolId();
        String phaseId = reqModel.phase;
        String subjectId = reqModel.subject;
        Page<Paper> page = null;
        Pageable pageable = reqModel.getPageable(Direction.DESC, "createdDate");
        if (StringUtils.isNotBlank(phaseId) && StringUtils.isNotBlank(subjectId)) {
            // 按学段+学科
            page = paperRepository.findByStatusAndPhaseIdAndSubjectIdAndUserIdAndSchoolId(1, phaseId, subjectId, userId,
                    schoolId, pageable);
        } else if (StringUtils.isNotBlank(phaseId) && StringUtils.isBlank(subjectId)) {
            // 按学段
            page = paperRepository.findByStatusAndPhaseIdAndUserIdAndSchoolId(1, phaseId, userId, schoolId, pageable);
        } else {
            // 全部
            page = paperRepository.findByStatusAndUserIdAndSchoolId(1, userId, schoolId, pageable);
        }

        RespModel respModel = null;
        List<RespModel> list = new ArrayList<RespModel>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        if (page != null && CollectionUtils.isNotEmpty(page.getContent())) {
            for (Paper paper : page.getContent()) {
                respModel = new RespModel();
                respModel.id = paper.getId();
                respModel.title = paper.getTitle();
                respModel.category = (paper.getPhaseName() + paper.getSubjectName());
                respModel.createDate = sdf.format(paper.getCreatedDate());
                list.add(respModel);
            }
        }
        long total = 0;
        if (page != null) {
            total = page.getTotalElements();
        }
        Page<RespModel> pageImpl = new PageImpl<RespModel>(list, pageable, total);
        return pageImpl;
    }

    protected static class ReqModel extends YxyApiPageReqModel {
        // 学段
        public String phase;
        // 学科
        public String subject;
    }

    protected static class RespModel extends YxyApiRespModel {
        // 试卷id
        public String id;
        // 试卷标题
        public String title;
        // 试卷分类
        public String category;
        // 组卷时间
        public String createDate;
    }

}
